from selenium.webdriver.common.by import By

from common.Uilt import forum_driver


class home:
    def __init__(self):
        driver = ""
        self.driver = forum_driver.driver
    #未登录访问主页使用
    def home_no_login(self):
        #self.driver.get("http://127.0.0.1:58080/index.html")
        self.driver.find_element(By.CSS_SELECTOR,"#index_nav_avatar").click()
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_logout").click()
        self.driver.get("http://127.0.0.1:58080/index.html")

        # mytext = self.driver.find_element(By.CSS_SELECTOR,"#main-message > h1 > span").text
        # assert mytext == "无法访问此网站"

        forum_driver.getwinshot()

    # 已登录使用主页
    def home_login(self):
        self.driver.get("http://127.0.0.1:58080/sign-in.html")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("asd")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        text1 = self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a > span.nav-link-title").text
        assert text1 == "首页" #说明登录后首页可以使用
        forum_driver.getwinshot()

    # 点击文章
    def home_login_click(self):
        self.driver.get("http://127.0.0.1:58080/sign-in.html")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("asd")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()


        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(3) > div > div.col > div.text-truncate > a > strong").click()
        text2 = self.driver.find_element(By.CSS_SELECTOR, "#article_details_author_name").text

        assert text2 == "我是测试人员" #说明登录后首页可以使用
        forum_driver.getwinshot()


home = home()
home.home_login()

home.home_no_login()

home.home_login_click()
home.driver.quit()

